446A - DZY Loves Sequences - CodeForces Solution


dp implementation two pointers *1600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
#define lld long double
#define fi first
#define se second
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vvi vector<vector<int>>
#define vpii vector<pair<int,int>>
#define vvpii vector<vector<pair<int,int>>>
#define mii map<int,int>
#define pii pair<int,int>
#define pq priority_queue
#define setbits(a) __builtin_popcountll(a) 
#define ctz(a) __builtin_ctzll(a)
#define clz(a) __builtin_clzll(a)
#define loop(n) for(int i=0;i<n;i++)
#define fo(i,s,e) for(int i=s;i<e;i++)
#define inn(a,n) int n; cin>>n; vector<int> a(n); for(int i=0;i<n;i++) cin>>a[i];
#define ink(a,n,k) int n,k; cin>>n>>k; vector<int> a(n); for(int i=0;i<n;i++) cin>>a[i];
#define test int t; cin>>t; while(t--)
#define sz(a) (int)a.size()
#define sortf(a) sort(a.begin(),a.end())
#define sortr(a) sort(a.rbegin(),a.rend())
#define all(a) a.begin(),a.end()
#define gsum(a) accumulate(a.begin(),a.end(),0LL)
#define gmax(a) *max_element(a.begin(),a.end())
#define gmin(a) *min_element(a.begin(),a.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
const int mod = 1000000007;

const int N = 200001, inf = 900000000000000;
const lld eps = 1e-6, pi = 3.1415926535897932;
const long long INF = 1e18+5;

//********PEN CHALAO PAPER PAI SAWAL HO SAKTA HAI****************//


void solve(){
        int n;
        cin>>n;
        vi v(n+1);
        for(int i=1; i<=n; i++)cin>>v[i];

        int leftmax[n+1];
        int rightmax[n+1];
        rightmax[n+1]=0;
        leftmax[0]=0;
        for(int i=1; i<n+1; i++){
            if(i==1 || v[i]<=v[i-1]){
                leftmax[i]=1;
            }else{
                leftmax[i]=1+leftmax[i-1];
            }
        }


        for(int j=n; j>=1; j--){
            if(j==n || v[j]>=v[j+1]){
                rightmax[j]=1;
            }else{
                rightmax[j]=1+rightmax[j+1];
            }
        }




       // for(int i=1; i<n+1; i++)cout<<leftmax[i]<<" ";
       // cout<<"\n";
       // for(int j=1; j<n+1; j++)cout<<rightmax[j]<<" ";

        int ans =1;
        if(n==2)ans =2;

        for(int j=1; j<=n; j++){
            if(v[j+1]-v[j-1]>=2){
                ans = max(ans, leftmax[j-1]+rightmax[j+1]+1);
            }else{
                ans = max(ans, leftmax[j-1]+1);
                ans = max(ans, rightmax[j+1]+1);
            }
        }

         cout<<ans<<endl;
        
}


int32_t main()   
{

    fastio;
    //code goes here
     int t=1;
     //cin>>t;
   while(t--){
      solve();
   }



}


Comments

Submit
0 Comments
More Questions

766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament